package com.kotlin.flong.base

class ReifiedBox<T : Animals> {

    /**
     * 内联与类型擦除
     */
    inline fun <reified T> randomOrBackUp(block: () -> T): T {
        val list = listOf(
            Cat("大花猫", 10),
            Dog("大黑狗", 16)
        )
        //随机获取一个动物对象数据
        val item = list.shuffled().first()

        return if (item is T) {
            item
        } else {
            block()
        }
    }

}

open class Animals(val age: Int)
class Dog(val name: String, age: Int) : Animals(age){
    override fun toString(): String {
        return "Dog(name='$name',age='$age')"
    }
}
class Cat(val name: String, age: Int) : Animals(age){
    override fun toString(): String {
        return "Cat(name='$name',age='$age')"
    }
}


fun main() {
    val box: ReifiedBox<Cat> = ReifiedBox()
    val an = box.randomOrBackUp {
        Dog("小花狗", 15)
    }
    println(an)
}

